package gd.hq.yolov5.comunicate;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import gd.hq.yolov5.App;
import gd.hq.yolov5.R;

public class BluetoothActivity extends AppCompatActivity {
    private TextView textView;

    private Button search;
    private Button connect;
    private Button send;
    private Button disconnect;

    BleBroadCastRevciver broadcastReceiver = null;
    BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
    private String mac = "20:20:04:26:34:96";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble);
        textView = findViewById(R.id.tv);
        search = findViewById(R.id.serach);
        connect = findViewById(R.id.connect);
        send = findViewById(R.id.send);
        disconnect = findViewById(R.id.disconnect);

        if (defaultAdapter == null ){
            Log.d("TAG","此设备不支持蓝牙");
        }
        if(!defaultAdapter.isEnabled()){
            //蓝牙未打开，开启蓝牙
            startActivityForResult(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE),1);
        }
        registerBroadcast();

//        if(App.BLE_DEVICE!=null){
//            textView.setText("蓝牙已连接，若断开请重启APP，再次连接");
//        }

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchBlueTooth();
            }
        });

        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE = new MyBlueDevice(broadcastReceiver.getB_device());
                if(App.BLE_DEVICE.connect()){
                    textView.setText("连接成功");
                }else {
                    textView.setText("连接失败");
                }

            }
        });

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("l");
            }
        });

        disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("r");
            }
        });


        findViewById(R.id.up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("f");
            }
        });
        findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("l");

            }
        });
        findViewById(R.id.right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("r");

            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.BLE_DEVICE.sendMsg("s");

            }
        });
    }


    public void registerBroadcast(){
        broadcastReceiver = new BleBroadCastRevciver(mac,this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(broadcastReceiver, intentFilter);
    }

    /**
     * 搜索蓝牙
     */
    public void searchBlueTooth() {
        if (defaultAdapter != null) {
            Log.d("TAG", "搜索蓝牙");
            if (!defaultAdapter.isDiscovering()) {
                defaultAdapter.startDiscovery();
            }
        }
    }

    public void setText(String text){
        textView.setText(text);
    }

//    private void bondDevice(){
//        BluetoothDevice device = null;
//        if (defaultAdapter != null) {
//            device = defaultAdapter.getRemoteDevice(mac);
//        }
//        if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
//            Log.d("TAG", "已配对");
//        } else {
//            Log.d("TAG", "配对");
//            Method method = null;
//            try {
//                method = BluetoothDevice.class.getMethod("createBond");
//                method.invoke(device);
//            } catch (NoSuchMethodException e) {
//                e.printStackTrace();
//            } catch (IllegalAccessException e) {
//                e.printStackTrace();
//            } catch (InvocationTargetException e) {
//                e.printStackTrace();
//            }
//        }

//    }
}
